package com.example.project_01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.project_01.commons.Result;
import com.example.project_01.dto.DrugDTO;
import com.example.project_01.mapper.DrugMapper;
import com.example.project_01.model.DrugModel;
import com.example.project_01.pojo.Drug;
import com.example.project_01.pojo.Sale;
import com.example.project_01.service.DrugService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Queue;

@Service
public class DrugServiceImpl implements DrugService {

    @Autowired
    DrugMapper drugMapper;

    //获取所有药品信息并分页，drugName不为空则模糊查询
    @Override
    public PageInfo<DrugModel> getDrugWithPage(Integer pn, Integer size, String drugName) {
        // 参数验证
        if (pn == null || pn < 1) {
            pn = 1;  // 默认第一页
        }
        if (size == null || size < 1) {
            size = 10;  // 默认每页10条
        }
        if (size > 100) {
            size = 100;  // 限制最大每页100条，防止过大数据量
        }
        PageHelper.startPage(pn, size);
        List<DrugModel> list = drugMapper.getAllDrug(drugName);
        PageInfo<DrugModel> info = new PageInfo<>(list,5);
        return info;
    }

    //添加药品信息
    @Override
    public Result addDrug(DrugDTO drugDTO) {
        //设置创建时间和更新时间
        drugDTO.setCreateTime(new Date());
        drugDTO.setUpdateTime(new Date());

        //执行添加药品信息操作
        int row = drugMapper.addDrug(drugDTO);
        if (drugDTO.getDrugId() == null) {
            return new Result(500, "药品添加失败，未获取到药品ID", null);
        }
        //添加药店信息
        int row2 = drugMapper.insertSalePlace(drugDTO.getDrugId(),drugDTO.getSaleIds());
        //获取所有药品的数据
        List<DrugModel> allDrug = drugMapper.getAllDrug(null);
        //插入成功后返回总记录数
        drugDTO.setTotal((long) allDrug.size());
        //如果能被5整除，则每页五条记录，否则就6条记录
        if (row >0 && row2 > 0) {
            Long num = drugDTO.getTotal()%5 == 0 ? (drugDTO.getTotal()/5) : (drugDTO.getTotal()/5)+1;
            return new Result(200,"药品添加成功",num);
        }
        return new Result(200,"药品添加失败",null);

    }

    //根据药品id删除药品信息和售卖地点
    @Override
    public Result deleteDrug(Integer drugId) {
        if (drugId == null){
            return new Result(201,"找不到要删除的id");
        }

        //删除售卖药品的店信息
        int row2 = drugMapper.deleteSaleByDrugId(drugId);
        //删除药品信息
        QueryWrapper<Drug> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("drug_id",drugId);
        int row1 = drugMapper.delete(wrapper1);

        if (row1 > 0 && row2 > 0) {
            return new Result(200,"删除成功");
        } else if (row2 > 0) {
            return new Result(202,"成功删除售卖药品的店铺信息");
        } else if (row1 > 0) {
            return new Result(203,"成功删除药品信息");
        }

        return new Result(204,"删除失败");
    }

    //根据id更新药店数据
    @Override
    public Result updateDrug(DrugDTO drugDTO) {
        if (drugDTO == null){
            return new Result(201,"修改的数据不能为空");
        }

        //====执行修改操作====
        UpdateWrapper<Drug> wrapper = new UpdateWrapper<>();
        wrapper.eq("drug_id",drugDTO.getDrugId())
                .set("drug_name",drugDTO.getDrugName())
                .set("drug_info",drugDTO.getDrugInfo())
                .set("drug_effect",drugDTO.getDrugEffect())
                .set("drug_img",drugDTO.getDrugImg())
                .set("publisher",drugDTO.getDrugPublisher())
                .set("updatetime",new Date());
        int row = drugMapper.update(wrapper);
        if (row > 0) {
            return new Result(200,"药品信息更新成功",drugDTO);
        }
        return new Result(202,"药品信息更新失败");
    }


}
